package dhm.com.dhmshop.updata.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import dhm.com.dhmshop.R;
import dhm.com.dhmshop.adapter.mine.MessageAdapter;
import dhm.com.dhmshop.base.BaseActiity;
import dhm.com.dhmshop.base.Presenter.PressenterImpl;
import dhm.com.dhmshop.base.netWork.Constant;
import dhm.com.dhmshop.base.netWork.LoginContract;
import dhm.com.dhmshop.entity.Bean;
import dhm.com.dhmshop.entity.MessageListBean;
import dhm.com.dhmshop.utils.SpUtils;

public class MessageActivity extends BaseActiity implements LoginContract.IView {

    @BindView(R.id.recy_message)
    XRecyclerView recyMessage;
    @BindView(R.id.allcheck)
    CheckBox allcheck;
    @BindView(R.id.linearimage)
    LinearLayout linearimage;
    private MessageAdapter messageAdapter;

    int page = 1;
    private PressenterImpl pressenter;
    private List<MessageListBean.DataBean> messageListBean;

    @Override
    protected int getLayout() {
        return R.layout.activity_message;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        pressenter = new PressenterImpl();
        pressenter.attachView(this);
        page = 1;
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);


        recyMessage.setLayoutManager(manager);

        recyMessage.setLoadingMoreEnabled(true);
        recyMessage.setPullRefreshEnabled(true);

        recyMessage.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page = 1;
                getdata();
            }

            @Override
            public void onLoadMore() {
                getdata();
            }
        });

        getdata();

    }

    @Override
    protected void onResume() {
        super.onResume();
        page=1;
        getdata();
    }

    private void getdata() {
        String uid = SpUtils.getString(this, "uid");
        Map<String, String> map = new HashMap<>();
        map.put("token", Constant.TOKEN);
        map.put("uid", uid);
        map.put("page", page + "");
        pressenter.sendMessage(this, Constant.MyMessageList, map, MessageListBean.class);
    }

    @Override
    protected void initData() {

    }

    @Override
    public void requesta(Object data) {
        if (data instanceof MessageListBean) {
            MessageListBean messageListBeans = (MessageListBean) data;
            if (messageListBeans.getCode() == 1) {
                linearimage.setVisibility(View.GONE);
                if (page == 1) {
                    messageListBean = messageListBeans.getData();
                    messageAdapter = new MessageAdapter(this);
                    messageAdapter.setaddClick(new MessageAdapter.OnIntentClick() {
                        @Override
                        public void item(List<MessageListBean.DataBean> message) {
                            messageListBean = message;
                            boolean check=true;
                            for (int i=0;i<message.size();i++){
                                if (!message.get(i).isChecked()){
                                    check=false;
                                    i=message.size();
                                }
                            }
                            allcheck.setChecked(check);
                        }
                    });
                    recyMessage.setAdapter(messageAdapter);
                } else {
                    messageListBean.addAll(messageListBeans.getData());
                }


                messageAdapter.setShopList(messageListBean);
            }else {
                if (page==1){
                    linearimage.setVisibility(View.VISIBLE);
                }else {
                    Toast.makeText(this, "暂无更多了", Toast.LENGTH_SHORT).show();
                }
            }

            page++;
            recyMessage.loadMoreComplete();
            recyMessage.refreshComplete();

        }
        if (data instanceof Bean){
            Bean bean= (Bean) data;
            Toast.makeText(this, bean.getMessage(), Toast.LENGTH_SHORT).show();
            if (bean.getCode()==1){
                page=1;
                getdata();
            }


        }

    }

    @Override
    public void fail(String error) {

    }

    @OnClick()
    public void onViewClicked() {
        finish();
    }

    @OnClick({R.id.allcheck, R.id.btn_jie, R.id.back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.allcheck:
                for (int i = 0; i < messageListBean.size(); i++) {
                    messageListBean.get(i).setChecked(allcheck.isChecked());
                }
                messageAdapter.setShopList(messageListBean);
                break;
            case R.id.btn_jie:
                String messageid = "";
                for (int i = 0; i < messageListBean.size(); i++) {
                    if (messageListBean.get(i).isChecked()) {
                        messageid = messageid + messageListBean.get(i).getId() + ",";
                    }
                }
                if (messageid.equals("")) {
                    Toast.makeText(this, "请选择要删除的消息", Toast.LENGTH_SHORT).show();
                    return;
                }

                messageid = messageid.substring(0, messageid.length() - 1);
                String uid = SpUtils.getString(this, "uid");
                Map<String, String> map = new HashMap<>();
                map.put("token", Constant.TOKEN);
                map.put("uid", uid);
                map.put("ids", messageid);
                pressenter.sendMessage(this, Constant.DelMessage, map, Bean.class);


                break;
            case R.id.back:
                finish();
            default:
                break;
        }
    }
}
